package com.yumei.trader.gym.view.element;


import com.yumei.trader.gym.object.BaseObj;
import com.yumei.trader.gym.util.DateUtil;
import com.yumei.trader.gym.util.StreamWriter;

import org.json.JSONObject;

import java.math.BigDecimal;

/**
 * Ｋ线单元
 * Created by wykai on 16/1/29.
 */
public class TicketElement extends BaseObj {

    public String date; //日期
    public long dateLong; //日期时间戳

    public float close; //现价
    public float open;//今开价
    public float high; //最高价
    public float low; //最低价

    public float volume; //成交量
    public float amount; //成交额

    //MA
    public float MA5;
    public float MA10;
    public float MA20;
    public float MA30;
    //MACD
    public float DIF;
    public float DEA;
    public float MACD; // DIF - EDA

    //RSI
    public float RSI1;
    public float RSI2;
    public float RSI3;

    //DMI
    public float PDI; // DI+
    public float MDI; // DI-
    public float DX;
    public float ADX;

    //SAR
    public float SAR;

    //Bollinger Bands
    public float U_Boll;
    public float M_Boll;
    public float L_Boll;

    //KDJ
    public float Kn;
    public float Dn;
    public float Jn;

    public float ms1;


    //买卖点标记
    public int bs=0;
    //用户买卖点
    public int flag; // 1:买，－1:卖
    //用户操作类型
    public String type = "";


   //{"date":"2014-04-08","close":5.21,"open":5.18,"high":5.22,"low":5.16,"share":518604.0,"turnover":0.0}
    @Override
    public void parseJson(JSONObject jsonObj) throws Exception {

        date = jsonObj.optString("date","");

        String closeStr = jsonObj.optString("nominal","0");
        close = Float.parseFloat(closeStr);

        String openStr = jsonObj.optString("open","0");
        open = Float.parseFloat(openStr);

        String highStr = jsonObj.optString("high","0");
        high = Float.parseFloat(highStr);

        String lowStr = jsonObj.optString("low","0");
        low = Float.parseFloat(lowStr);


        String shareStr = jsonObj.optString("share","0");
        volume = Float.parseFloat(shareStr)/100;

        String turnoverStr = jsonObj.optString("turnover","0");
        amount = Float.parseFloat(turnoverStr);

        //System.out.println(toString());
    }




    ////{"stockCode":null,
    // "date":"2017-12-05",
    // "open":"41.721557938644814",
    // "high":"43.028450593581006",
    // "close":"43.008649189718334",
    // "low":"41.71165723671348",
    // "volume":7497485.0,
    // "amount":"0",

    // "ma10":42.626484,
    // "ma30":42.673676,

    // "macd":-0.21631488,
    // "macdsignal":-0.2989044,
    // "macdhist":0.082589515,

    // "rsi":0.5354186,

    // "upperBB":45.39069,
    // "middleBB":43.083893,
    // "lowerBB":40.7771,
    //
    // "slowK":0.5893117,
    // "slowD":0.38757974,
    // "mom":-1.7722256,
    //
    // "sar":40.800793,
    // "adx":0.18013775,
    // "ms":"1.0",
    // "ms1":-0.99938804}


    /**
     * 解析智能K线价格信息
     * @param jsonObj
     * @throws Exception
     */
    public void parseJsonOfIntelligence(JSONObject jsonObj) throws Exception {

        String dateStr = jsonObj.optString("date","");
        dateLong = Long.valueOf(dateStr);
        date = DateUtil.getDateTimeInMillis1(dateLong);

        String closeStr = jsonObj.optString("close","0");
        close = fexFloat(Float.parseFloat(closeStr), 2);

        String openStr = jsonObj.optString("open","0");
        open = fexFloat(Float.parseFloat(openStr), 2);

        String highStr = jsonObj.optString("high","0");
        high = fexFloat(Float.parseFloat(highStr), 2);

        String lowStr = jsonObj.optString("low","0");
        low = fexFloat(Float.parseFloat(lowStr), 2);

        String shareStr = jsonObj.optString("volume","0");
        volume = fexFloat(Float.parseFloat(shareStr)/100, 2);

        String turnoverStr = jsonObj.optString("amount","0");
        amount = fexFloat(Float.parseFloat(turnoverStr), 2);

        String msStr = jsonObj.optString("ms1","0");
        ms1 = Float.parseFloat(msStr);

        //System.out.println(toString());
    }

    private float fexFloat(float a, int length){

        BigDecimal decimal = new BigDecimal(a);
        return decimal.setScale(length, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public void parseBody(byte[] BodyBuffer, int p) {

        int pcount = p;

        this.date = StreamWriter.readInt(BodyBuffer, pcount)+""; //  YYYYMMDD
        pcount += StreamWriter.IntSize;


        this.open = StreamWriter.readFloat32(BodyBuffer, pcount);
        pcount += StreamWriter.FloatSize;

        this.high = StreamWriter.readFloat32(BodyBuffer, pcount);
        pcount += StreamWriter.FloatSize;

        this.low = StreamWriter.readFloat32(BodyBuffer, pcount);
        pcount += StreamWriter.FloatSize;

        this.close = StreamWriter.readFloat32(BodyBuffer, pcount);
        pcount += StreamWriter.FloatSize;

        this.volume = StreamWriter.readFloat32(BodyBuffer, pcount);
        pcount += StreamWriter.FloatSize;

        this.amount = StreamWriter.readFloat32(BodyBuffer, pcount);
        pcount += StreamWriter.FloatSize;

        //System.out.println(toString());

    }

    public String toString() {
        return "date=[" + this.date + "]\t" + "open =[" + open +
                "]\thigh =[" + high + "]\tlow =[" + low + "]\t" +
                "close =[" + close + "]\tshare =[" + volume +
                "]\t\nturnover =[" + amount + "]\n";
    }

}
